package com.fp.voiceshoper;

import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import com.fp.fpShoper.R;

public class SetAlarmActivity extends Activity implements OnClickListener {

	TimePicker timePicker1;
	DatePicker datePicker1;
	 @Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.set_alarm_layout);
	   
	    

	    
	    timePicker1=(TimePicker)findViewById(R.id.timePicker1);
	    timePicker1.setIs24HourView(true);
	    	    
	    datePicker1 =(DatePicker)findViewById(R.id.datePicker1);
	    
	   long at=getIntent().getLongExtra("Atim", 0);
	   Calendar cal=Calendar.getInstance();
	   
	    if (at>=System.currentTimeMillis())
	    {    	
	    	 cal.setTimeInMillis(at);
 	   
	    }
	    else{	    	
	    	 cal.setTimeInMillis(System.currentTimeMillis());    
	    }
	    
	    int year=cal.get(Calendar.YEAR);
 	    int month=cal.get(Calendar.MONTH);
 	    int day=cal.get(Calendar.DAY_OF_MONTH);
 	    int hour=cal.get(Calendar.HOUR_OF_DAY);
 	    int min=cal.get(Calendar.MINUTE);

 	    datePicker1.updateDate(year, month, day);

 	    timePicker1.setCurrentHour(hour);
 	    timePicker1.setCurrentMinute(min);
	    
	    Button tmmpB=(Button)findViewById(R.id.saveEdited);
	    tmmpB.setOnClickListener(this);
	    
	    tmmpB=(Button)findViewById(R.id.cancelEdit);
	    tmmpB.setOnClickListener(this);
	  }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		switch (v.getId()) {
		case R.id.saveEdited:
			Calendar calendar = Calendar.getInstance();
			calendar.set(datePicker1.getYear(), datePicker1.getMonth(),
					datePicker1.getDayOfMonth(), timePicker1.getCurrentHour(),
					timePicker1.getCurrentMinute(), 0);
			
			//int offset = calendar.getTimeZone().getOffset(calendar.getTimeInMillis());
			long startTime  = calendar.getTimeInMillis();
//			Toast.makeText(getApplicationContext(), String.valueOf(startTime-System.currentTimeMillis()), Toast.LENGTH_LONG).show();	
			intent.putExtra("AlarmTime", startTime);
			setResult(1, intent);
			finish();
			break;
		case R.id.cancelEdit:
			setResult(-1);
			finish();
			break;
		default:
			break;
		}
		
	}
	 
		 
}
